﻿using JinJun.EF.Permission.BLL;
using JinJun.EF.Permission.Model;
using JinJun.EF.Permission.Web.Attributes;
using JinJun.EF.Permission.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.EF.Permission.Web.Controllers
{
    [PermissionAttribute]
    public class AdminUserController : Controller
    {
        //创建业务层对象
        private AdminUserService adminUserService = new AdminUserService();

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        //public JsonResult GetModuleList(int page, int limit, string name)
        //{
        //    //创建分页类对象
        //    PageListResult<AdminUser> pageListResult = new PageListResult<AdminUser>();
        //    //赋值
        //    pageListResult.code = 0;
        //    pageListResult.msg = string.Empty;
        //    int count = 0;
        //    //创建以Module为类型的List对象
        //    List<AdminUser> modules = new List<AdminUser>();
        //    //表达式
        //    Expression<Func<AdminUser, bool>> whereLambda = a => a.Name.Contains(name);
        //    Expression<Func<AdminUser, int>> orderbyLambda = a => a.Id;
        //    //调用分页查询
        //    pageListResult.data = adminUserService.QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
        //    return Json(pageListResult);
        //}


        /// <summary>
        /// 获取组集合
        /// </summary>
        /// <returns></returns>
        public JsonResult Getmodulelist()
        {
            PageListResult<Module> pageListResult = new PageListResult<Module>();
            Expression<Func<Module, bool>> whereLambda = a => a.Id > 0;
            pageListResult.data = adminUserService.Getmodulelist(whereLambda);
            return Json(pageListResult);
        }


        /// <summary>
        /// 根据组Id查询对应的角色名
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult Getrolename(ModuleRole moduleRole)
        {
            PageListResult<Role> pageListResult = new PageListResult<Role>();
            pageListResult.data = adminUserService.Getrolename(moduleRole);
            return Json(pageListResult);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult Update(AdminUser adminUser)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = adminUserService.Update(adminUser);
            return Json(operateResult);
        }


        /// <summary>
        /// 多表添加--AdminUser,Role,Module
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>        
        public JsonResult AddAdminUser(Adu adu)
        {
            if (adu.Id == 0) {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            AdminUser adminUser = new AdminUser();
            adminUser.Name = adu.Name;
            adminUser.Password = adu.Password;
            adminUser.Email = adu.Email;
            adminUser.Remark = adu.Remark;
            adminUser.CreateTime = DateTime.Now;
            adminUser.LastLoginTime = DateTime.Now;
            Expression<Func<AdminUser, bool>> whereLambda = a => a.Name.Contains(adu.Name);
            //调用业务层添加方法
            operateResult.jg = adminUserService.Add(adminUser, adu.RoleId,adu.ModuleId, whereLambda);
                return Json(operateResult);
            }
            else
            {
                //更新
                OperateResult operateResult = new OperateResult();
                AdminUser adminUser = new AdminUser();
                adminUser.Id = adu.Id;
                adminUser.Name = adu.Name;
                adminUser.Password = adu.Password;
                adminUser.Email = adu.Email;
                adminUser.Remark = adu.Remark;
                adminUser.Password = adu.Password;
                adminUser.LastLoginTime = DateTime.Now;
                adminUser.CreateTime = DateTime.Now;
                //调用业务层添加方法
                operateResult.jg = adminUserService.AdminUpdate(adminUser, adu.RoleId, adu.ModuleId,adu.ARId,adu.MAId);
                return Json(operateResult);
            }

           
        }

        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="adu"></param>
        /// <returns></returns>
        public JsonResult Admindelete(Adu adu)
        {
            OperateResult operateResult = new OperateResult();
            AdminUser adminUser = new AdminUser();
            adminUser.Id = adu.Id;
            adminUser.Name = adu.Name;
            operateResult.Success = adminUserService.Admindelete(adminUser,adu.ARId, adu.MAId);
            return Json(operateResult);
        }



        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult Add(AdminUser module)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = adminUserService.Add(module);
            return Json(operateResult);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult Delete(AdminUser adminUser)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = adminUserService.Delete(adminUser);
            return Json(operateResult);
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult BatchDelete(List<AdminUser> adminUser)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = adminUserService.BatchDelete(adminUser);
            return Json(operateResult);
        }


        /// <summary>
        /// 删除多个实体根据Id
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteById(int[] ids)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = adminUserService.BatchDeleteById(ids);
            return Json(operateResult);
        }

        public JsonResult GetAdminUserList(int page, int limit,string username, string adddate)
        {
            //创建分页类对象
            PageListResult<AdminUserQuery> pageListResult = new PageListResult<AdminUserQuery>();
            List<AdminUserQuery> admins= new List<AdminUserQuery>();
            admins= adminUserService.GetAdminUserList(username,adddate);
            //赋值
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            pageListResult.data = admins.Skip((page - 1) * limit).Take(limit).ToList();
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
    }
}